package com.yxw.live_vod_boot.controller;

import com.yxw.live_vod_boot.entity.YxEmojiEntity;
import com.yxw.live_vod_boot.service.EmojiService;
import com.yxw.live_vod_boot.vo.EmojiEntityVO;
import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "表情包管理", tags = "表情包管理")
@Slf4j
@RequestMapping("/api/emojis")
public class EmojiController {

    @Autowired
    private EmojiService emojiService;

    /**
     * 获取所有表情包
     */
    @ApiOperation(value = "获取所有表情包")
    @PostMapping("/getAllEmojis")
    public BaseResult getAllEmojis() {
        List<EmojiEntityVO> emojis = emojiService.getAll();
        return BaseResult.doSuccess(emojis, "获取成功");
    }

    /**
     * 创建表情包
     */
    @ApiOperation(value = "创建表情包")
    @PostMapping("/createEmoji")
    public BaseResult createEmoji(@RequestBody @Validated YxEmojiEntity emoji) {
        emojiService.save(emoji);
        return BaseResult.doSuccess(null, "创建成功");
    }

    /**
     * 根据表情ID获取表情包
     */
    @ApiOperation(value = "根据表情ID获取表情包")
    @PostMapping("/getEmojiById")
    public BaseResult getEmojiById(@RequestParam("emojiId") Long emojiId) {
        YxEmojiEntity emoji = emojiService.getById(emojiId);
        if (emoji == null) {
            return BaseResult.failed(ErrorCodeEnum.ID_ERROR.getMessage());
        } else {
            return BaseResult.doSuccess(emoji, "获取成功");
        }
    }

    /**
     * 更新表情包
     */
    @ApiOperation(value = "更新表情包")
    @PostMapping("/updateEmoji")
    public BaseResult updateEmoji(@RequestParam("emojiId") Long emojiId, @RequestBody @Validated YxEmojiEntity emoji) {
        YxEmojiEntity existingEmoji = emojiService.getById(emojiId);
        if (existingEmoji == null) {
            return BaseResult.failed(ErrorCodeEnum.ID_ERROR.getMessage());
        }
        emoji.setEmojiId(emojiId);
        emojiService.update(emoji);
        return BaseResult.doSuccess(null, "更新成功");
    }

    /**
     * 删除表情包
     */
    @ApiOperation(value = "删除表情包")
    @PostMapping("/deleteEmoji")
    public BaseResult deleteEmoji(@RequestParam("emojiId") Long emojiId) {
        YxEmojiEntity existingEmoji = emojiService.getById(emojiId);
        if (existingEmoji == null) {
            return BaseResult.failed(ErrorCodeEnum.ID_ERROR.getMessage());
        }
        emojiService.deleteById(emojiId);
        return BaseResult.doSuccess(null, "删除成功");
    }
}